package com.javabasic.algorithm.leetcode;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/10/11 11:40 下午
 * @see
 */
public class IntegerToEnglishWords {

    public static void main(String[] args) {
        // 2^31-1 = 2 147 483 647
        System.out.println(Integer.MAX_VALUE);
    }


    /**
     * 暴力解法，数字字母转换
     * @param num
     * @return
     */
    public String numberToWords(int num) {
        if (num == 0) return "Zero";
        int[] dp = new int[10];
        int len = 0;
        while (num > 0) {
            dp[len++] = num%10;
            num = num/10;
        }
        for (int i = len-1; i >= 0; i--) {
            System.out.print(dp[i] + " ");
        }
        System.out.println();
//        for (int i = 0; i < len/2; i++) {
//            int tag = dp[i];
//            dp[i] = dp[len-1-i];
//            dp[len-1-i] = tag;
//        }
        int segment;
        if (len%3 == 0) {
            segment = len/3;
        } else {
            segment = len/3+1;
        }

        String[] segmentUnix = {"","Thousand","Million","Billion"};
        String[] numEn = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve",
        "Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
        String[] numEn2 = {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};


        StringBuilder[] result = new StringBuilder[segment];
        for (int i = 0; i < len; i += 3) {
            int segmentIndex = i / 3;
            result[segmentIndex] = new StringBuilder();
            int tagValue = 0;
            if (i+2 < len && dp[i+2] != 0) {
                result[segmentIndex].append(" " + numEn[dp[i+2]] + " Hundred");
                tagValue += dp[i+2]*100;
            }
            if (i+1 < len && dp[i+1] > 1) {
                tagValue += dp[i+1]*10 + dp[i];
                result[segmentIndex].append(" " + numEn2[dp[i+1]]);
                if (dp[i] > 0) {
                    result[segmentIndex].append(" " + numEn[dp[i]]);
                }
            } else if (i+1 < len && dp[i+1] == 1) {
                int value = dp[i + 1] * 10 + dp[i];
                tagValue += value;
                result[segmentIndex].append(" " + numEn[value]);
            } else if (dp[i] != 0) {
                tagValue += dp[i];
                result[segmentIndex].append(" " + numEn[dp[i]]);
            }
            if (segmentIndex > 0 &&  tagValue > 0) {
                result[segmentIndex].append(" " + segmentUnix[segmentIndex]);
            }
        }
        String resultStr = "";
        for (int i = segment-1; i >= 0; i--) {
            resultStr += result[i];
        }
        return resultStr.trim();
    }
}
